include("circles.lua")
local commands = {
	{ command="ulx Ban|You Go Now and Do Not Come back|0", text="Ban" },
	{ command="ulx kick|GTFO", text="Kick" },
	{ command="ulx astrip", text="Astrip" }, 
	{ command="ulx give", text="Give" },
	{ command={
		{ command="ulx rocket", text="Rocket"},
		{ command="ulx explode", text="Explode"},
		{ command="ulx slay", text="Slay"},
		{ command="ulx maul", text="Maul"},
		{ command="ulx slap", text="Slap"},
		{ command="ulx ragdoll", text="Ragdoll"},
		{ command="ulx unragdoll", text="UnRagdoll"},
		{ command="ulx jail", text="Jail"},
		{ command="ulx unjail", text="Unjail"}
	}, text="Fun"},
	{ command={
		{command = "ulx kick|Spamming", text="Spamming"},
		{command = "ulx kick|Annoying", text="Annoying"},
		{command = "ulx kick|Shutup", text="Shutup"},
		{command = "ulx kick|Read the rules", text="Rules"},
		{command = "ulx kick|Not Understanding English", text="No English"},
		{command = "ulx kick|No you don't need that much shit", text="Too Much Shit"},
		{command = "ulx kick|Respect Admins.", text="Respect Admins"},
		{command = "ulx kick|Mic Spam", text="Mic spam"}
	}, text="Kicks"},
	{ command={
		{command = "ulx ban|Calm Down for 10 Minutes Then Come Back|10", text="10 Min Ban"},
		{command = "ulx ban|Calm Down for 30 Minutes Then Come Back|30", text="30 Min Ban"},
		{command = "ulx ban|Think about what you have done for an hour then come back|60", text="60 Min Ban"}
	}, text="Bans"}
	
}



local size = 200 -- Size of menu
local text_offset = 150
local gap = 60
local boundingbox_margin = 10
local boundingbox_color = Color( 255, 255, 255, 70 )
local text_color = Color(0,0,0,255)
local textbg_color = Color(50,50,75,0)
local subMenu_color = Color(255,0,0,255)
local subMenu_bgcolor = Color(50,50,75,0)
local text_width = 3.15 -- Magic number, trial and error
local text_height = draw.GetFontHeight("DefaultLarge") 
local ply_target

local function hideRadialMenu( ply )
	hook.Remove( "HUDPaint", "ULXDrawRadial" )
	gui.EnableScreenClicker( false )
	selection = commands
end
concommand.Add( "-ulxradial", hideRadialMenu )

local function drawRadialMenu()
	if not selection then
		selection = commands
	end
	if type(selection) ~= "table" then
		selection = commands
	end
	-- Gets the center of the screen
	local origin_x = ScrW() / 2.0
	local origin_y = ScrH() / 2.0
	
	-- Our bounding box
	--epcircle.DrawCircle(originX,originY,radius,thick,startAng,distAng,iter)
	surface.SetDrawColor(255,255,255,70)
	epcircle.DrawCircle(origin_x,origin_y,size,nil,0,360,32)
	
	-- The amount of angle (in rad) each command gets
	local slice = 2 * math.pi / table.Count(selection)
	
	-- Draw each portion of our pie and label it
	for i, val in ipairs( selection ) do
		local ang = slice * i -- get our current angle
		
		-- Draw the line
		local start_x = math.sin( ang ) * gap + origin_x
		local start_y = math.cos( ang ) * gap + origin_y
		local end_x = math.sin( ang ) * size + origin_x
		local end_y = math.cos( ang ) * size + origin_y
		surface.SetDrawColor( 0, 0, 0, 255 )
		surface.DrawLine( start_x, start_y, end_x, end_y )
		
		-- Draw the text
		local text_ang = ang - slice/2
		local text_x = math.sin( text_ang ) * text_offset + origin_x - val.text:len() * text_width
		local text_y = math.cos( text_ang ) * text_offset + origin_y - text_height
		if type(val.command) == "table" then
			draw.WordBox( 8, text_x, text_y, val.text, "DefaultBold", subMenu_bgcolor, subMenu_color )
		else
			draw.WordBox( 8, text_x, text_y, val.text, "DefaultLarge", textbg_color, text_color )
		end
	end
	
	if not ply_target:IsValid() or not ply_target:IsPlayer() then
		hideRadialMenu()
	else
		-- Lock onto the player
		local diff = ply_target:GetPos() - Vector( 0, 0, 16 ) - LocalPlayer():GetPos()
		LocalPlayer():SetEyeAngles( diff:Angle() )
		
		-- Draw the player nick at the center of the pie
		local nick = ply_target:Nick()
		draw.WordBox( 8, origin_x - nick:len() * text_width, origin_y - text_height, nick, "DefaultLarge", textbg_color, text_color )
	end
end

local function showRadialMenu( ply )
	local trace = util.GetPlayerTrace( LocalPlayer() )
	local trace_results = util.TraceLine( trace )

	if not trace_results.Entity:IsValid() or not trace_results.Entity:IsPlayer() then
		-- Try finding a best choice
		local best_choice
		local best_choice_diff
		local localply_pos = LocalPlayer():GetPos()
		local localply_ang = LocalPlayer():GetAimVector():Angle()
		local players = player.GetAll()
		for i, ply in ipairs( players ) do
			if ply ~= LocalPlayer() then
				local vec_diff = ply:GetPos() - Vector( 0, 0, 16 ) - localply_pos
				local ang = vec_diff:Angle()
				local diff = math.abs( math.NormalizeAngle( ang.pitch - localply_ang.pitch ) ) + math.abs( math.NormalizeAngle( ang.yaw - localply_ang.yaw ) )
				if not best_choice_diff or diff < best_choice_diff then
					best_choice_diff = diff
					best_choice = ply
				end
			end
		end
		
		if not best_choice_diff or best_choice_diff > 12 then
			return -- Give up
		end
		ply_target = best_choice
	else
		ply_target = trace_results.Entity
	end
	hook.Add( "HUDPaint", "ULXDrawRadial", drawRadialMenu )
	gui.EnableScreenClicker( true )
end
concommand.Add( "+ulxradial", showRadialMenu )



local function radialAction(mc)
if not ply_target then return end
	local origin_x = ScrW() / 2.0
	local origin_y = ScrH() / 2.0
	
	local mouse_x, mouse_y = gui.MousePos()
	mouse_x = mouse_x - origin_x 
	mouse_y = mouse_y - origin_y
	if mouse_x*mouse_x + mouse_y*mouse_y < gap*gap then
		selection = commands
		showRadialMenu(  )
		return
	else
		local mouse_ang = math.atan2( mouse_x, mouse_y )
		if mouse_ang < 0 then
			mouse_ang = math.pi * 2 + mouse_ang
		end
		local slice = 2 * math.pi / table.Count(selection)
		local num_chosen = math.ceil( mouse_ang / slice )
		
		local command = selection[ num_chosen ].command or nil
		if not command then return false end
		if type(command) == "table" then
			--hook.Remove( "HUDPaint", "ULXDrawRadial" )
			selection = command
			showRadialMenu(  )
			return true
		end
		local construct = string.Explode("|",command)
		if construct[3] and construct[2] and construct[1] then
			LocalPlayer():ConCommand( construct[1] .. " \"" .. ply_target:Nick() .."\"".." "..construct[3].." \""..construct[2].."\"")
			return true
		elseif construct[2] and construct[1] then
			LocalPlayer():ConCommand( construct[1] .. " \"" .. ply_target:Nick() .."\"".." \""..construct[2].."\"")
			return true
		elseif construct[1] then
			LocalPlayer():ConCommand( construct[1] .. " \"" .. ply_target:Nick() .. "\"")	
			return true
		else
			LocalPlayer():ConCommand( "ulx csay \"ahref fucked up Please tell him about this\"" )
			return false
		end
	end
end
hook.Add("GUIMousePressed","ulxradialmousepressed",radialAction)

function checkStatus(v)
		local status = {}
		if v.jail then
			table.insert( status, "jail" )
		end
		if v.ragdoll then
				table.insert( status, "ragdoll" )
		end
		return status
end
	


